<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<style>
	body,html{
		margin: 0 auto;
		height:100%;
		width:100%;
	}
</style>
</head>
<body>
	<div id="sys_areaMain" style="width:100%;height:100%;overflow: auto;" class="divfrist">
			<DIV id="" style="width:100%;height:0.7%;overflow:hidden;"></DIV>
			<DIV id="" style="width:100%;height:98.6%;">
					<DIV id="" class="" style="float:left;width:0.4%;height:100%;overflow:hidden;"></DIV>
					<DIV id="" class="" style="float:left;width:20%;height:100%;">
							<DIV id="" class="allborder" style="width:100%;height:100%;max-height:100%;max-width:100%;overflow:auto;">
							   <!--内容区 -->
								 <ul id="addarea_tree" class="ztree"></ul>	
							   <!--内容区 -->
							</DIV>
					</DIV>
					<DIV id="" class="" style="float:left;width:0.4%;height:100%;overflow:hidden;"></DIV>
					<DIV id="" class="" style="float:left;width:78.8%;height:100%;">
							<DIV id="" class="allborder" style="width:100%;height:100%;max-height:100%;max-width:100%;">
								<!-- 内容区 -->
											<div style="width:100%;height:100%;">		
													 <table  id="areaTable" style="border: 0px; width: 100%"></table>
											</div>
											<div id="area_tb" style="overflow: hidden; padding-left: 10px; padding-top: 5px;padding-bottom: 5px;" >
												 <a href="javascript:void(0);" class="easyui-linkbutton"   data-options="iconCls:'icon-add',plain:true" onclick="window.area.addarea()">增加区域</a>||	
												 <a href="javascript:void(0);" class="easyui-linkbutton"   data-options="iconCls:'icon-edit',plain:true" onclick="window.area.updatearea()">编辑区域</a>||
												 <a href="javascript:void(0);" class="easyui-linkbutton"   data-options="iconCls:'icon-no',plain:true" onclick="window.area.deletearea()">删除区域</a>||	    	
											</div>		
								<!-- 内容区 -->
							</DIV>
					</DIV>
					<DIV id="" class="" style="float:left;width:0.37%;height:100%;overflow:hidden;"></DIV>
			</DIV>
			<DIV id="" class="div_bottom"></DIV>
</div>
<script type="text/javascript">
(function(){
	//增加区域
	function addarea(){
		var treeObj = $.fn.zTree.getZTreeObj("addarea_tree");
		var nodes = treeObj.getSelectedNodes();
		if(nodes.length<=0){
			alert("未选中一个区域,不能编辑");
			return;
		}
		
		if($("#area_addarea").length>0){
			$("#area_addarea").window("open");
		}
		$('<div id="area_addarea"></div>').window({
		    title: '增加区域', 		
		    iconCls:'icon-edit', 
		    width:650,   
		    height:400,   
		    
		    resizable:true,
		    closed: true,   
		    draggable:false,
		    modal: true,
		    content:$('<div style="width:100%;height:100%;"></div>').load("${pageContext.request.contextPath}/html/system/area_add.jsp"),
		    onClose:function(){
				$("#area_addarea").window("destroy",true);
				 createTree();
		    }
		});
		$("#area_addarea").window("open");
		
	}
	//更新区域
	function updatearea(){
		var treeObj = $.fn.zTree.getZTreeObj("addarea_tree");
		var nodes = treeObj.getSelectedNodes();
		
		if(nodes.length<=0){
			alert("未选中一个区域,做为新增区域的上级区域");
			return;
		}
		if(nodes[0].id==1){
			alert("不能更新根节点 ");
			return;
		}
		if($("#area_updatearea").length>0){
			$("#area_updatearea").window("open");
		}
		$('<div id="area_updatearea"></div>').window({
		    title: '编辑区域', 		
		    iconCls:'icon-add', 
		    width:650,   
		    height:400,   
		    resizable:true,
		    closed: true,   
		    draggable:false,
		    modal: true,
		    content:$('<div style="width:100%;height:100%;"></div>').load("${pageContext.request.contextPath}/html/system/area_add.jsp?areaId="+nodes[0].uuid),
		    onClose:function(){
				$("#area_updatearea").window("destroy",true);
				 createTree();
		    }
		});
		$("#area_updatearea").window("open");
		
	}
	//树的点击事件
	function onClick(event, treeId, treeNode){
		$.ajax({
			   type: "POST",
			   url: "${pageContext.request.contextPath}/sys/sysAreaAction!getChildrenarea.action",
			   data:{areaTID:treeNode.id},
			   dataType:'json',
			   success: function(msg){
				   $('#areaTable').datagrid("loadData",msg);
			   }
		});	
		
	}
	//创建树
	function createTree(treeDate){
		var setting = {
				data: {
					simpleData: {
						enable: true
					}
				},
				callback: {
					onClick: onClick
				}
		};
		if(treeDate==null||treeDate.length==0){
			$.ajax({
				   type: "POST",
				   url: "${pageContext.request.contextPath}/sys/sysAreaAction!getareaTree.action",
				   data:{roleId:0},
				   dataType:'json',
				   success: function(msg){
					   var node = new Object();
					   node.uuid=0;
					   node.id=1;
					   node.pid=0;
					   node.name="根区域";
					   node.open=true;
					   msg.push(node);
					   $.fn.zTree.init($("#addarea_tree"), setting, msg);     
				   }
			});	
		}else{
			 $.fn.zTree.init($("#addarea_tree"), setting, treeDate);    
		}
	}
	//创建区域table
	function createareaTable(){
			$('#areaTable').datagrid({
				fit: true,
				toolbar:"#area_tb",
				singleSelect:false,
				rownumbers: true,
				animate:true,
				border:false,
				striped: true,
				loadMsg:'正在加载数据',
				collapsible:true,
				idField:'areaId',
				fitColumns:true,
				columns:[[
			  		{field:'areaName',title:'区域名称',width:120},
			  		{field:'areaPost',title:'区域邮编',width:120},
			  		{field:'areaNote',title:'区域描述',width:120},
			  		]]
					//pagination:true
				});
	}
	//删除一个区域和所有子区域
	function deletearea(){
		var treeObj = $.fn.zTree.getZTreeObj("addarea_tree");
		var nodes = treeObj.getSelectedNodes();
		if(nodes.length<=0){
			alert("未在左侧树节点中选择一个区域");
			return;
		}
		$.messager.confirm('删除区域', '删除区域，会删除区域下的所有子区域,你确定你需要删除这些区域?', function(r){
			if (r){
				$.ajax({
					   type: "POST",
					   url: "${pageContext.request.contextPath}/sys/sysAreaAction!deletearea.action",
					   data:{areaTID:nodes[0].id},
					   dataType:'text',
					   success: function(msg){
						   if(msg=="success"){
							   alert("删除成功");
							   createTree();
							   $('#areaTable').datagrid("loadData",[]);
						   }else{
							   alert("删除失败")
						   }
					   }
				});	
			}
		});
	}
	//注册所有事件
	window["area"]={};
	window["area"]["addarea"]=addarea;
	window["area"]["updatearea"]=updatearea;
	window["area"]["deletearea"]=deletearea;
	$(function(){	
		$.parser.parse('#sys_areaMain'); 
		createareaTable();
		createTree(null);
		 
	});
})();
</script>
</body>
</html>